//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by yang on 15/11/12.
//  Copyright © 2015年 yang. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let imageCount = 4
class NewFeatureViewController: UICollectionViewController {

    
    init(){
    let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout:layout)
        //分页
        collectionView?.pagingEnabled = true
        //滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        //弹簧效果
         collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        if page == imageCount - 1{
            if let cell = collectionView?.visibleCells().last as?NewFeatureCell{
            cell.startAnimation()
            }
        }
    }
}
class NewFeatureCell :UICollectionViewCell{
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加子控件
    private func setupUI(){
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
        make.bottom.equalTo(contentView).offset(-200)
        }
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        
    }
    //startButton 的点击方法
    @objc private func start(){
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    private lazy var iconImage :UIImageView = UIImageView()
    private lazy var startButton : UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
    //重写 index 设置图片
    var index:Int = 0{
        didSet{
        iconImage.image = UIImage(named: "new_feature_\(index + 1)")
        startButton.hidden = true
        }
    }
    //最后一页 startButton 显示动画
    func startAnimation(){
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        self.startButton.hidden = false
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                
        }
    }
}